// 用户设备数据存储文件
// 逆变器

#include "com_user_data.h"

// ---------------- 全局变量初始化为默认值 ----------------
Pvinterver_t gInverterData = {
    .factory_code = FactoryCODE_JL,
    .adder = 0x01,
    .PhV_phsA = 230.0,
    .PhV_phsB = 231.0,
    .PhV_phsC = 229.5,
    .I_phsA = 10.5,
    .I_phsB = 10.8,
    .I_phsC = 9.9};

// 打印逆变器数据
void Pvinterver_Print(void)
{
    // 打印标题
    cm_log_printf(0, "Inverter Phase Data:");

    // 单独打印每相电压和电流
    cm_log_printf(0, "  Voltage A: %dV", gInverterData.PhV_phsA);
    cm_log_printf(0, "  Voltage B: %dV", gInverterData.PhV_phsB);
    cm_log_printf(0, "  Voltage C: %dV", gInverterData.PhV_phsC);

    cm_log_printf(0, "  Current A: %dA", gInverterData.I_phsA);
    cm_log_printf(0, "  Current B: %dA", gInverterData.I_phsB);
    cm_log_printf(0, "  Current C: %dA", gInverterData.I_phsC);
}

// ========================================================================
// 品牌1: 固德威 (GDW) 的轮询表和解析函数
// 假设：
// - 功能码 0x03, 寄存器 0x0000 读取 3个寄存器 (A,B,C相电压)
// - 功能码 0x03, 寄存器 0x0006 读取 3个寄存器 (A,B,C相电流)
// ========================================================================
void Parse_GDW_PhaseVoltage(uint8_t *buf, uint16_t len)
{
}

void Parse_GDW_PhaseCurrent(uint8_t *buf, uint16_t len)
{
}

const ModbusPollItem_t pollTable_GDW[] = {
    {0x03, 0x0000, 3, 2000, 2, Parse_GDW_PhaseVoltage}, // 电压
    {0x03, 0x0006, 3, 2000, 2, Parse_GDW_PhaseCurrent}  // 电流
};
const uint8_t pollCount_GDW = sizeof(pollTable_GDW) / sizeof(ModbusPollItem_t);

// 品牌2: 锦浪 (JL) 的轮询表和解析函数
const ModbusPollItem_t pollTable_JL[] = {
    {0x04, 3033, 10, 2000, 2, Parse_JL_01}, // 3033 读取10个
    {0x04, 3004, 10, 2000, 2, Parse_JL_02}  // 3004 读取10个
};
const uint8_t pollCount_JL = sizeof(pollTable_JL) / sizeof(ModbusPollItem_t);
void Parse_JL_01(uint8_t *buf, uint16_t len)
{

    uint16_t phaseA_voltage = getDataOfu16(buf, 3);
    gInverterData.PhV_phsA = phaseA_voltage;

    uint16_t phaseB_voltage = getDataOfu16(buf, 5);
    gInverterData.PhV_phsB = phaseB_voltage;

    uint16_t phaseC_voltage = getDataOfu16(buf, 7);
    gInverterData.PhV_phsC = phaseC_voltage;

    uint16_t phaseA_current = getDataOfu16(buf, 9);
    gInverterData.I_phsA = phaseA_current;

    uint16_t phaseB_current = getDataOfu16(buf, 11);
    gInverterData.I_phsB = phaseB_current;

    uint16_t phaseC_current = getDataOfu16(buf, 13);
    gInverterData.I_phsC = phaseC_current;

    Pvinterver_Print();
}
void Parse_JL_02(uint8_t *buf, uint16_t len)
{
    /*
  3004 读取0x10个
  有功功率
  总发电量1kWh
  当月发电量
  上月发电量
  当日发电量0.1kWh

  */
}

// ========================================================================
// 品牌3: 阳光电源 (YG) 的轮询表和解析函数
// ... (类似地实现其他品牌)
// ========================================================================
void Parse_YG_SystemStatus(uint8_t *buf, uint16_t len)
{
}
const ModbusPollItem_t pollTable_YG[] = {
    {0x03, 0x0000, 10, 3000, 2, Parse_YG_SystemStatus},
    // ... 其他轮询项
};
const uint8_t pollCount_YG = sizeof(pollTable_YG) / sizeof(ModbusPollItem_t);

// ========================================================================
// 厂家配置映射表
// 将每个厂家代码与它的轮询表和表长度关联起来
// ========================================================================
const FactoryConfig_t factory_config_map[FactoryCODE_MAX] = {
    {FactoryCODE_GDW, pollTable_GDW, pollCount_GDW},
    {FactoryCODE_JL, pollTable_JL, pollCount_JL},
    {FactoryCODE_SH, NULL, 0},          // 暂未实现
    {FactoryCODE_Growatt_USB, NULL, 0}, // 暂未实现
    {FactoryCODE_YG, pollTable_YG, pollCount_YG},
    {FactoryCODE_AoTai, NULL, 0}, // 暂未实现
    {FactoryCODE_DY, NULL, 0}     // 暂未实现
};